#!/usr/bin/perl -w
#for processing pvaluelooper inputs
#combines muscle alignment with peak distance data
#input list of files containing peak distance data (intended for a set where the human sequence is always first in the fasta file)
#opens each distance file and corresponding alignment file (where alignment for each sequence is fasta formatted)
#matches each sequence based on numbered tags
#creates output file for each set
#starts each output file with human sequence ID
#next line prints set of headers separated by tabs
#for distance data/alignment splice:
	#where "-" is present in alignment, prints "-"
	#where amino acid is present, prints corresponding distance value
	#for first 8 and last 8 amino acids, prints "-" (based on %MinMax windowsize = 17, so no values for 8 at ends)

$INPUT = $ARGV[0];
open(FILE,$INPUT);

$ERROR = $ARGV[1];
open(ERROR,">$ERROR");

while($filename = <FILE>){
	chomp $filename;
	$aln_file = $filename;
	$aln_file =~ s/peaks.dist/prot.muscle.aln2/;
	#print $aln_file,"\n";
	unless(open(ALN,$aln_file)){print " failed to open $aln_file\n";}
	$output = $filename . "\.spliced";
	open(OUTPUT, ">$output");
#	@peaks = ();
	@headers = ();
	@alignment = ();
	#print $aln_file,"\n";
	@alignments = ();
	while ($sequence = <ALN>){
		chomp $sequence;
		$alignment = <ALN>;
		chomp $alignment;
		@sequence = split(" ",$sequence);
		$tag = shift @sequence;
		if($tag =~ "@"){@tag = split('\|',$tag);$tag = shift @tag;}
		#print $tag,"\n";
		push(@alignments,$tag);
		push(@alignments,$alignment);
	}
	$dist_file = $filename;
	unless(open(DIST,$dist_file)){print "failed to open $dist_file\n";}
	#print "distance\n";
	$find = 0;
	$found = 0;
	@headers = ();
	while ($header = <DIST>){
		chomp $header;
		++$find;
		push(@headers,$header);push(@headers,"\t");
		$distance = <DIST>;
		chomp $distance;
		@distance = split(",",$distance);
		@header = split(" ",$header);
		$tag = shift @header;
		if ($tag =~ "@"){@tag = split('\|',$tag);$tag = shift @tag;print OUTPUT $tag,"\n";}
		$place = 0;
		$aligndata = 0;
		foreach $aln (@alignments){
			++$place;
			if ($aln eq $tag){$aligndata = $alignments[$place];++$found;}
		}
		$end = 0;
		@splice = ();
		#if (@aligndata eq "0"){print ERROR "alignment not found $filename\n";}
		@aligndata = split('',$aligndata);
		$length = scalar @aligndata;
		until (scalar @aligndata == 0){
			$aln = shift @aligndata;
			if ($aln eq "-"){push(@splice,$aln);push(@splice,"\t");}
			else {
				++$end;
				if (($end < 9) or scalar @distance == 0){
					push(@splice,"-");push(@splice,"\t");
				}
				else {$distance = shift @distance; push(@splice,$distance);push(@splice,"\t");
				}
			}
		}
	print OUTPUT $header,"\n";
	print OUTPUT @splice,"\n";
	$finished = scalar @splice;
	unless ($length == $finished/2){print "$filename $tag $length $alignment but $finished finished\n";}
        unless ($length == $finished/2){print ERROR "$filename $tag $length $alignment but $finished finished\n";}
	}
	unless ($find == $found){print "$filename looking for $find but found $found\n";}
        unless ($find == $found){print ERROR "$filename looking for $find but found $found\n";}
	#print $found,"\n";print $find,"\n";


#exit;
close OUTPUT;
close DIST;
close ALN;
}
close FILE;
exit;
